<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 首页
 *
 * @author      Eden
 * @datetime    2016/12/17 21:54
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Slide_Model extends API_Model
{

    private $slideNum = 4;

    private $width = 640;
    private $height = 360;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();

        try {
            $config = $this->_getConfigByKey('general', 'value');
            if (!empty($config) && !empty($config['value'])) {
                $config = json_decode($config['value'], true);
                $this->slideNum = !empty($config['slideNum']) ? intval($config['slideNum']) : $this->slideNum;
            }
        } catch(Exception $e){
            $this->output->send(array(
                'state' => false,
                'error' => '配置错误'
            ));
        }
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        $send['state'] = true;
        if ($res = $this->_getSlide()) {
            $send['data'] = $res;
        } else {
            $send['data'] = [];
        }
        $this->output->send($send);
    }

    /**
     * 请求幻灯列表
     */
    private function _getSlide()
    {
        $db = clone($this->db);
        $q = $db->select('b.id, b.name, b.thumb, b.liveUrl, b.streamId, b.created')
            ->limit($this->slideNum)
            ->order_by('modified', 'DESC')
            ->order_by('created', 'DESC')
            ->where('status', 1)
            ->from('base as b')
            ->get();
        $rows = $q->result_array();
        $return = array();
        if (!empty($rows)) foreach ($rows as $item) {
            $return[] = array(
                'baseId' => intval($item['id']),
                'baseName' => value($item, 'name'),
                'thumb' => thumb($item['thumb'], $this->width, $this->height),
                'created' => formateTimeStamp($item['created']),
                'isLive' => !empty($item['liveUrl']),
                'liveUrl' => value($item, 'liveUrl', '')
            );
        } else {
            return [];
        }

        return $return;
    }

    /**
     * 直播流
     * @param $rows
     * @param $ids
     * @return mixed
     */
    private function _getStreams($rows, $ids)
    {
        if (!empty($ids)) {
            $ids = array_unique($ids);
            $ids = array_values($ids);
            $q = $this->db->select('s.id, s.videoUrlHD, s.videoUrlSD')
                ->from('stream_device as s')
                ->where(array('status' => 1))
                ->where_in('id', $ids)
                ->get();
            $streams = $q->result_array();
            $streamUrls = [];
            if (!empty($streams)) foreach($streams as $s) {
                $id = $s['id'];
                $streamUrls[$id] = array(
                    'videlUrlHD' => value($s, 'videoUrlHD', ''),
                    'videoUrlSD' => value($s, 'videoUrlSD', '')
                );
            }

            if (!empty($streamUrls)) foreach($rows as &$r) {
                $streamId = $r['streamId'];
                unset($r['streamId']);
                if (!empty($streamId) && array_key_exists($streamId, $streamUrls)) {
                    $r['live'] = $streamUrls[$streamId];
                }
            }
        }
        return $rows;
    }

}

